/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.commands;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.TemplateSignature;

import com.cea.papyrus.core.helper.IGraphElementHelper;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;

//TODO: Auto-generated Javadoc
//TODO: Auto-generated Javadoc
/**
 * The Class Di2UMLElementCreateCommand.
 * 
 * @author Patrick Tessier
 * @since 23 nov. 06
 * 
 * this class is used to add the di2 element to another graph element
 */
public class Di2AddCommand extends PapyrusCommand {

	/**
	 * The location.
	 */
	private Point location=null;

	/**
	 * The size.
	 */
	private Dimension size=null;

	/**
	 * The uml parent.
	 */
	private Element newUmlParent;

	/**
	 * The uml parent.
	 */
	private Element oldUmlParent;
	
	/**
	 * The uml child.
	 */
	private Element umlChild=null;

	/**
	 * The parent GE.
	 */
	private GraphElement newParentGE;

	/**
	 * The parent GE.
	 */
	private GraphElement oldParentGE;

	/**
	 * the created graphElement*.
	 */
	private GraphElement childGe;

	/**
	 * The Constructor.
	 * 
	 * @param element the element
	 * @param typeDiagram 
	 * @param childGe 
	 * @param parent the parent
	 */
	public Di2AddCommand(GraphElement childGe, GraphElement parent,String typeDiagram) {
		super(((Uml1SemanticModelBridge)childGe.getSemanticModel()).getElement().getClass().getInterfaces()[0]);
		this.imodelerFactory= ModelerHelper.getFactory(typeDiagram);
		this.elementFactory = this.imodelerFactory.getElementFactory(this.elementClass);
		
		
		this.newParentGE=parent;
		this.childGe=childGe;

		this.oldParentGE=childGe.getContainer();

		try{
			this.oldUmlParent=LookForElement.lookForUMLElementOf(oldParentGE);
			this.newUmlParent=LookForElement.lookForUMLElementOf(newParentGE);
		}
		catch (Exception e) {
			Debug.log(e);
		}
		this.setLabel("\"addition of graphElement \"");
	}

	/**
	 * use it when you manipulate a node with semantic parent if not the undo not run.
	 * 
	 * @param oldParent 
	 */
	public void setOldParent(GraphElement oldParent){
		this.oldParentGE=oldParent;
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	public boolean canExecute() {
		return ((IGraphElementHelper)elementFactory).canCreateGraphElement(this.newParentGE);
	}

	/**
	 * Execute.
	 */
	public void execute(){
		((IGraphElementHelper)elementFactory).addGraphElement(this.childGe, this.newParentGE);
	}

	/**
	 * Redo.
	 */
	public void redo(){
		((IGraphElementHelper)elementFactory).addGraphElement(this.childGe,this.newParentGE);
	}

	/**
	 * Undo.
	 */
	public void undo(){
		//((IGraphElementHelper)elementFactory).addGraphElement(this.childGe, this.oldParentGE);
		((IGraphElementHelper)elementFactory).removeGraphElement(this.childGe, this.newParentGE);
	}

	/**
	 * @return <code>true</code> if the command can be undone. This method should only be
	 * called after <code>execute()</code> or <code>redo()</code> has been called.
	 */
	public boolean canUndo() {
		//TODO: To be corrected in version 1.7
		if (childGe.getSemanticModel() != null)
			if (((Uml1SemanticModelBridge)childGe.getSemanticModel()).getElement() != null)
					if (((Uml1SemanticModelBridge)childGe.getSemanticModel()).getElement() instanceof TemplateSignature)
						return false ;
		return true;
	}
	
}
